<?php
    class PacientesController extends AppController {
        var $name = 'Pacientes';
        var $uses = array('Paciente');
        var $helpers = array('Javascript');
        var $components = array('AdminParameters');

        function index(){

            $pacientes = $this->__getListData();

            $this->set("pacientes", $pacientes);

            $this->render("index");
        }

        function datosPersonales(){
            $this->render("datos_personales", "ajax");
        }

        function  getList($page=0, $limit=10, $orderColumn='apellido', $ascDesc='ASC'){

            $pacientes = $this->__getListData($this->params["form"], $page, $limit, $orderColumn, $ascDesc);

            $this->set("pacientes", $pacientes);

            $this->render("listado_pacientes", "ajax");
        }

        function getListData($page=0, $limit=10, $orderColumn='apellido', $ascDesc='ASC'){
            $pacientes = $this->__getListData($this->params["form"], $page, $limit, $orderColumn, $ascDesc);

            $this->set("result", $pacientes);

            $this->render("/elements/ajax_return", "ajax");
        }

        function __getListData($params = array(), $page=0, $limit=10, $orderColumn='apellido', $ascDesc='ASC'){

            $model = "Paciente.";

            $fields = array(
                 $model."id", $model."nombre", $model."apellido"
            );

            $result = $this->AdminParameters->getListData($this->Paciente, $fields, $params, $page, $limit, $orderColumn, $ascDesc);

            return $result;

        }

        function getDetail($op, $id){

            $this->set("operation", $op);
            $this->set("editId", $id);

            $this->render("datos_personales", "ajax");

            }

        function getDetailData($id){
            $data = $this->Paciente->find("all", array("conditions" => array("Paciente.id" => $id), "recursive" => 0));

            $this->set("result", array("data" => $data));

            $this->render("/elements/ajax_return", "ajax");
        }

        function save(){

            if($this->AdminParameters->doSave($this->Paciente, $this->params["form"])){
                $this->set("result", array("result" => true));
            }else{
                $this->set("result", array("result" => false));
                $this->set("result", array("error" => "Error while saving data."));
            }


            $this->render("/elements/ajax_return", "ajax");
            }

        function getOptions(){
                $data = $this->Paciente->find("list", array("fields" => array("Paciente.id", "Paciente.nombre")));

                $this->set("result", array("data" => $data));

                $this->render("/elements/ajax_return", "ajax");
            }
    }
?>